package com.apestech.framework.db;

import com.apestech.framework.json.JSONObject;

import java.util.Locale;

/**
 * @author hup
 * @create 2018-10-15-14:45
 */
public class FormatField {
    private int scale=2;  //小数位，默认2
    private String math="round";  //公式（ceil：向上取整1.1为2，  floor：向下取整1.1为1，  round：四舍五入不解释），默认round
    private String type="number";  //类型 （number数值类型， amount金额类型，percent百分比，permillage千分比，timeStamp：时间戳字串格式）默认number
    private Locale locale=Locale.getDefault();

    public FormatField(JSONObject json) throws Exception {
        if(json==null) return;
        if(json.get("scale")!=null) this.scale=json.getIntValue("scale");
        if(json.get("math")!=null) this.math=json.getString("math");
        if(json.get("type")!=null) this.type=json.getString("type");
        iniLocale(json.getString("locale"));
    }

    public FormatField(String type) throws Exception {
        this.type=type;
    }

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }

    public String getMath() {
        return math;
    }

    public void setMath(String math) {
        this.math = math;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(String l) {
        iniLocale(l);
    }

    private void iniLocale(String l){
        if(l!=null){
            switch (l){
                case "cn":{
                    this.locale=Locale.CHINA;
                    break;
                }
                case "en":{
                    this.locale=Locale.ENGLISH;
                    break;
                }
                case "fr":{
                    this.locale=Locale.FRENCH;
                    break;
                }
                case "de":{
                    this.locale=Locale.GERMAN;
                    break;
                }
                case "it":{
                    this.locale=Locale.ITALIAN;
                    break;
                }
                case "ja":{
                    this.locale=Locale.JAPANESE;
                    break;
                }
                case "ko":{
                    this.locale=Locale.KOREAN;
                    break;
                }
                case "zh":{
                    this.locale=Locale.SIMPLIFIED_CHINESE;
                    break;
                }
                case "tw":{
                    this.locale=Locale.TRADITIONAL_CHINESE;
                    break;
                }
                case "gb":{
                    this.locale=Locale.UK;
                    break;
                }
                case "us":{
                    this.locale=Locale.US;
                    break;
                }
                case "ca":{
                    this.locale=Locale.CANADA;
                    break;
                }
                case "frca":{
                    this.locale=Locale.CANADA_FRENCH;
                    break;
                }
            }
        }
    }

}
